{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cf5eaaba",
   "metadata": {},
   "source": [
    "# 本周作业\n",
    "## 1.请同学们利用python函数的方法，封装 face++ 人脸检测，人脸分析和人脸对比\n",
    "## 2.请同学们调用face++ 人脸搜索API接口和人脸库API组\n",
    "## 3.尝试应用 上述 2 ，设计宿舍人脸识别系统小应用（微信小程序尝试）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d008ce99",
   "metadata": {},
   "source": [
    "# python函数的方法，封装 face++ 人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "17cc0618",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-24T13:29:25.178662Z",
     "start_time": "2022-03-24T13:29:25.169677Z"
    }
   },
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "1b031f46",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-24T13:29:25.661539Z",
     "start_time": "2022-03-24T13:29:25.655553Z"
    }
   },
   "outputs": [],
   "source": [
    "API_key = 'wH5jJXa327R2iOJ9TtgHkMXS4WHnfIzj'\n",
    "API_secret = 'APfd1pOaneXua_QcYfmcZEZrXfPfm1BM'\n",
    "image_url_01='https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'  \n",
    "image_url_02='https://tse3-mm.cn.bing.net/th/id/OIP-C._MVPR376nHT7sR7DbefPcAHaHa?pid=ImgDet&rs=1'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "77f27d72",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-24T13:29:26.036703Z",
     "start_time": "2022-03-24T13:29:26.025346Z"
    }
   },
   "outputs": [],
   "source": [
    "def detect_face(API_key,API_secret,image_url,landmark=None,attributes=None,face_rectangle=None,beauty_score_min=0,beauty_score_max=100):\n",
    "    \"\"\"face++ 人脸检测接口函数(图片链接)\"\"\"\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload={\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_secret,\n",
    "        'image_url':image_url,\n",
    "        'return_landmark':landmark,\n",
    "        'return_attributes':attributes,\n",
    "        'face_rectangle':face_rectangle,\n",
    "        'beauty_score_min':beauty_score_min,\n",
    "        'beauty_score_max':beauty_score_max   \n",
    "    }\n",
    "    r = requests.post(url,payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "33700966",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-24T13:29:35.633557Z",
     "start_time": "2022-03-24T13:29:34.680338Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648128575,444077d4-dc95-4b2b-b0d3-c547ad92605b',\n",
       " 'time_used': 294,\n",
       " 'faces': [{'face_token': '30ea3a62e36678c33ddd4874134ff95d',\n",
       "   'face_rectangle': {'top': 108, 'left': 141, 'width': 107, 'height': 107}}],\n",
       " 'image_id': 'JoYdaZ0h1hGb4ih1d2EZVw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_secret,image_url_01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "0d63112b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-24T13:30:54.474081Z",
     "start_time": "2022-03-24T13:30:53.460535Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'297b39bb3dbd2224ea0d2dc6dc659bc7'"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token01 = detect_face(API_key,API_secret,image_url_01)[\"faces\"][0][\"face_token\"]\n",
    "face_token01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "09d617a2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-24T13:29:49.444311Z",
     "start_time": "2022-03-24T13:29:48.209348Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648128588,be09809e-b5af-447d-968e-b0dd0bb1e16e',\n",
       " 'time_used': 908,\n",
       " 'faces': [{'face_token': 'c282b4b15f3f5c46774bc1c5a99fc346',\n",
       "   'face_rectangle': {'top': 257, 'left': 421, 'width': 35, 'height': 35}},\n",
       "  {'face_token': '40ad1c2d3f9213c6e3257cccd1cd30cf',\n",
       "   'face_rectangle': {'top': 197, 'left': 5, 'width': 35, 'height': 35}},\n",
       "  {'face_token': 'a979e12dabc89b04a8d36716118807cb',\n",
       "   'face_rectangle': {'top': 316, 'left': 244, 'width': 35, 'height': 35}},\n",
       "  {'face_token': '3017ef727f6eb82c66e88dbbf0c8400b',\n",
       "   'face_rectangle': {'top': 80, 'left': 308, 'width': 35, 'height': 35}},\n",
       "  {'face_token': 'a314195948fd028e4136bb032aedab98',\n",
       "   'face_rectangle': {'top': 374, 'left': 421, 'width': 35, 'height': 35}},\n",
       "  {'face_token': 'c8150bc97341c870a1f30d6c921ae871',\n",
       "   'face_rectangle': {'top': 20, 'left': 186, 'width': 34, 'height': 34}},\n",
       "  {'face_token': 'de9fe2048c1bdf32788e8eed51b3f9ca',\n",
       "   'face_rectangle': {'top': 79, 'left': 422, 'width': 34, 'height': 34}},\n",
       "  {'face_token': 'c208c37c96b2db2964e1593c648e4ddd',\n",
       "   'face_rectangle': {'top': 316, 'left': 135, 'width': 34, 'height': 34}},\n",
       "  {'face_token': '847227daf2d6f77bc600dc09a2d5244e',\n",
       "   'face_rectangle': {'top': 21, 'left': 130, 'width': 33, 'height': 33}},\n",
       "  {'face_token': 'd7f9bbc6b45ee59a9544b725ccee12cb',\n",
       "   'face_rectangle': {'top': 316, 'left': 185, 'width': 33, 'height': 33}},\n",
       "  {'face_token': 'b312a8b16d8c0c5cb720fc62b13cf1cb',\n",
       "   'face_rectangle': {'top': 435, 'left': 362, 'width': 33, 'height': 33}},\n",
       "  {'face_token': '5146d4869359fbe6c772acaa6c15803d',\n",
       "   'face_rectangle': {'top': 258, 'left': 309, 'width': 33, 'height': 33}},\n",
       "  {'face_token': '866c3e5ce03ed9ef10bc4a90d273d799',\n",
       "   'face_rectangle': {'top': 20, 'left': 422, 'width': 33, 'height': 33}},\n",
       "  {'face_token': '5642351b070a1bc0dec8af108ee5f164',\n",
       "   'face_rectangle': {'top': 197, 'left': 76, 'width': 33, 'height': 33}},\n",
       "  {'face_token': '40be9d1490ec0a95015fc4d52335e6c0',\n",
       "   'face_rectangle': {'top': 198, 'left': 371, 'width': 33, 'height': 33}},\n",
       "  {'face_token': 'e37d7ca773bfae275b1f32e92d387d94',\n",
       "   'face_rectangle': {'top': 257, 'left': 16, 'width': 32, 'height': 32}},\n",
       "  {'face_token': '6c9ae752dd191db42735462235c40661',\n",
       "   'face_rectangle': {'top': 377, 'left': 307, 'width': 32, 'height': 32}},\n",
       "  {'face_token': '97c695ddb7ff7757e1d3aacb243b99c7',\n",
       "   'face_rectangle': {'top': 140, 'left': 428, 'width': 32, 'height': 32}},\n",
       "  {'face_token': '1591cb840016e02c86f1ed2edaa74863',\n",
       "   'face_rectangle': {'top': 81, 'left': 75, 'width': 32, 'height': 32}},\n",
       "  {'face_token': '8a3c5199d551cdc995b1347a8ec1cf39',\n",
       "   'face_rectangle': {'top': 377, 'left': 7, 'width': 32, 'height': 32}},\n",
       "  {'face_token': '779f928a9b227fd6c35f0129b46940f8',\n",
       "   'face_rectangle': {'top': 199, 'left': 188, 'width': 32, 'height': 32}},\n",
       "  {'face_token': '39f7d983f152244370404b55534a97c2',\n",
       "   'face_rectangle': {'top': 318, 'left': 10, 'width': 32, 'height': 32}},\n",
       "  {'face_token': '264b6d3ecded4dca7b3851b0f5f9f84b',\n",
       "   'face_rectangle': {'top': 318, 'left': 72, 'width': 32, 'height': 32}},\n",
       "  {'face_token': 'd350cd7af82c1dc052e026f07da54ada',\n",
       "   'face_rectangle': {'top': 198, 'left': 250, 'width': 32, 'height': 32}},\n",
       "  {'face_token': '175a1491576ff9d87d42718abee4f724',\n",
       "   'face_rectangle': {'top': 258, 'left': 370, 'width': 32, 'height': 32}},\n",
       "  {'face_token': '64cc4310d8136aaa0a65373e0c22b20e',\n",
       "   'face_rectangle': {'top': 317, 'left': 309, 'width': 32, 'height': 32}},\n",
       "  {'face_token': '7e560ad3442da727c53ce33544a9a93e',\n",
       "   'face_rectangle': {'top': 21, 'left': 373, 'width': 32, 'height': 32}},\n",
       "  {'face_token': 'b1f53e720719ac2dda4a22b1cbe452d4',\n",
       "   'face_rectangle': {'top': 140, 'left': 305, 'width': 32, 'height': 32}},\n",
       "  {'face_token': '9ee5d03f021d53588c9cce0f04aad35d',\n",
       "   'face_rectangle': {'top': 377, 'left': 132, 'width': 32, 'height': 32}},\n",
       "  {'face_token': 'a5680901d7cf4ac9448dc3dc9bf9dbd3',\n",
       "   'face_rectangle': {'top': 199, 'left': 313, 'width': 32, 'height': 32}},\n",
       "  {'face_token': '759fb546e517ad206baa7b6809c0eaa3',\n",
       "   'face_rectangle': {'top': 435, 'left': 194, 'width': 32, 'height': 32}},\n",
       "  {'face_token': '7cf161d46675ca88e07cd886d8b59519',\n",
       "   'face_rectangle': {'top': 376, 'left': 76, 'width': 32, 'height': 32}},\n",
       "  {'face_token': 'dd01468077746f0f6bcc6d782516e0eb',\n",
       "   'face_rectangle': {'top': 80, 'left': 133, 'width': 32, 'height': 32}},\n",
       "  {'face_token': '7b466ba1859a314a6aaf433d7b4504cb',\n",
       "   'face_rectangle': {'top': 436, 'left': 304, 'width': 32, 'height': 32}},\n",
       "  {'face_token': '182457c8705e4fa108b77ac05a48d78b',\n",
       "   'face_rectangle': {'top': 259, 'left': 72, 'width': 31, 'height': 31}},\n",
       "  {'face_token': 'f2991915b82e37992663ed5fc6a6c7cd',\n",
       "   'face_rectangle': {'top': 199, 'left': 427, 'width': 31, 'height': 31}},\n",
       "  {'face_token': '8bc95abc69f09d5101480750d569002d',\n",
       "   'face_rectangle': {'top': 22, 'left': 73, 'width': 31, 'height': 31}},\n",
       "  {'face_token': 'da373f75a138c9abee8fe561fcafb1b9',\n",
       "   'face_rectangle': {'top': 376, 'left': 367, 'width': 31, 'height': 31}},\n",
       "  {'face_token': '0fcdd4103f598f50c90e5238955ec602',\n",
       "   'face_rectangle': {'top': 258, 'left': 194, 'width': 31, 'height': 31}},\n",
       "  {'face_token': '205c220b20b7c44d5159df19b70ebc80',\n",
       "   'face_rectangle': {'top': 377, 'left': 192, 'width': 31, 'height': 31}},\n",
       "  {'face_token': '4e600a5904e32199881d8024405d5594',\n",
       "   'face_rectangle': {'top': 436, 'left': 428, 'width': 31, 'height': 31}},\n",
       "  {'face_token': 'e99fcd49ea5d7b7e843ebacc0ced4470',\n",
       "   'face_rectangle': {'top': 199, 'left': 131, 'width': 31, 'height': 31}},\n",
       "  {'face_token': '034b235648e32cbb149758a5fecef75b',\n",
       "   'face_rectangle': {'top': 319, 'left': 365, 'width': 31, 'height': 31}},\n",
       "  {'face_token': 'ba1eb740f358146f41701a9f81b9a1bb',\n",
       "   'face_rectangle': {'top': 377, 'left': 251, 'width': 31, 'height': 31}},\n",
       "  {'face_token': 'ac4b5bf120218942c67a224fd38fc5f2',\n",
       "   'face_rectangle': {'top': 437, 'left': 14, 'width': 31, 'height': 31}},\n",
       "  {'face_token': 'f99eaf3b75eaa0893016d22a35a1294d',\n",
       "   'face_rectangle': {'top': 140, 'left': 72, 'width': 30, 'height': 30}},\n",
       "  {'face_token': '21a8be26db23abfe5573d6d814c16aa8',\n",
       "   'face_rectangle': {'top': 80, 'left': 252, 'width': 30, 'height': 30}},\n",
       "  {'face_token': 'e5cb8bb3a637d7b47824af722961a712',\n",
       "   'face_rectangle': {'top': 23, 'left': 251, 'width': 30, 'height': 30}},\n",
       "  {'face_token': '384dccd48c6dfd9f6aa915aef4cbdc78',\n",
       "   'face_rectangle': {'top': 258, 'left': 252, 'width': 30, 'height': 30}},\n",
       "  {'face_token': 'ff38cbb09786355cf6dc85acba393458',\n",
       "   'face_rectangle': {'top': 318, 'left': 426, 'width': 30, 'height': 30}}],\n",
       " 'image_id': 'bezQ9W4DwND6mLX989LGHA==',\n",
       " 'face_num': 50}"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_secret,image_url_02)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "cef0b647",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-24T13:31:15.478869Z",
     "start_time": "2022-03-24T13:31:14.443776Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'f5ee1d3f20943c8951c27844819e625b'"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token02 = detect_face(API_key,API_secret,image_url_02)[\"faces\"][0][\"face_token\"]\n",
    "face_token02"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d91a0aa8",
   "metadata": {},
   "source": [
    "# python函数的方法，封装 face++ 人脸分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "49e95f7e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-24T14:04:05.577085Z",
     "start_time": "2022-03-24T14:04:05.574083Z"
    }
   },
   "outputs": [],
   "source": [
    "API_key = 'wH5jJXa327R2iOJ9TtgHkMXS4WHnfIzj'\n",
    "API_secret = 'APfd1pOaneXua_QcYfmcZEZrXfPfm1BM'\n",
    "face_token_01 = '297b39bb3dbd2224ea0d2dc6dc659bc7'\n",
    "face_token_02 = 'f5ee1d3f20943c8951c27844819e625b'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "b9965ee7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-24T14:04:07.634972Z",
     "start_time": "2022-03-24T14:04:07.627961Z"
    }
   },
   "outputs": [],
   "source": [
    "def analyze_face(API_Key,API_Secret,face_token):\n",
    "    '''face++ API人脸分析函数'''\n",
    "    analyze_url = \"https://api-cn.faceplusplus.com/facepp/v3/face/analyze\"\n",
    "    payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"face_tokens\":face_token,\n",
    "    \"return_attributes\":'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus',\n",
    "    'beauty_score_min':0,\n",
    "    'beauty_score_max':100\n",
    "\n",
    "    }\n",
    "    r_analyze = requests.post(url = analyze_url , params = payload)\n",
    "    return r_analyze.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "f57a7888",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-24T14:04:24.110277Z",
     "start_time": "2022-03-24T14:04:23.264931Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 226,\n",
       " 'request_id': '1648130663,403528a5-a7fe-4283-ade2-1e3fb8df3960',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.039,\n",
       "     'neutral': 0.001,\n",
       "     'disgust': 0.191,\n",
       "     'anger': 0.001,\n",
       "     'surprise': 0.031,\n",
       "     'fear': 0.001,\n",
       "     'happiness': 99.736},\n",
       "    'beauty': {'female_score': 58.555, 'male_score': 58.212},\n",
       "    'gender': {'value': 'Male'},\n",
       "    'age': {'value': 53},\n",
       "    'mouthstatus': {'close': 0.0,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 100.0,\n",
       "     'other_occlusion': 0.0},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 1.554,\n",
       "     'stain': 9.17,\n",
       "     'acne': 1.527,\n",
       "     'health': 11.352},\n",
       "    'headpose': {'yaw_angle': 2.7483263,\n",
       "     'pitch_angle': 14.900063,\n",
       "     'roll_angle': 0.5866398},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.376},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.376},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.376}},\n",
       "    'smile': {'threshold': 50.0, 'value': 100.0},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.005,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.995,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.003,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.001,\n",
       "      'no_glass_eye_open': 99.996,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 85.357},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.494,\n",
       "      'vector_z_component': 0.935,\n",
       "      'vector_x_component': -0.068,\n",
       "      'vector_y_component': 0.348,\n",
       "      'position_y_coordinate': 0.438},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.502,\n",
       "      'vector_z_component': 0.942,\n",
       "      'vector_x_component': 0.247,\n",
       "      'vector_y_component': 0.227,\n",
       "      'position_y_coordinate': 0.422}}},\n",
       "   'face_rectangle': {'width': 107, 'top': 108, 'left': 141, 'height': 107},\n",
       "   'face_token': '297b39bb3dbd2224ea0d2dc6dc659bc7'}]}"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "analyze_face(API_key,API_secret,face_token_01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "bbca03f0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-24T14:04:35.133688Z",
     "start_time": "2022-03-24T14:04:34.689244Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 214,\n",
       " 'request_id': '1648130674,489dac5e-1303-43a1-939d-dd1d86c37b0b',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 3.994,\n",
       "     'neutral': 93.582,\n",
       "     'disgust': 0.115,\n",
       "     'anger': 0.074,\n",
       "     'surprise': 0.236,\n",
       "     'fear': 0.074,\n",
       "     'happiness': 1.926},\n",
       "    'beauty': {'female_score': 59.515, 'male_score': 57.304},\n",
       "    'gender': {'value': 'Female'},\n",
       "    'age': {'value': 27},\n",
       "    'mouthstatus': {'close': 99.148,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 0.847,\n",
       "     'other_occlusion': 0.004},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 2.97,\n",
       "     'stain': 82.551,\n",
       "     'acne': 36.539,\n",
       "     'health': 5.99},\n",
       "    'headpose': {'yaw_angle': -22.076704,\n",
       "     'pitch_angle': 3.3816824,\n",
       "     'roll_angle': 0.8909828},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 99.061},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 99.061},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 99.061}},\n",
       "    'smile': {'threshold': 50.0, 'value': 0.717},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.679,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.006,\n",
       "      'no_glass_eye_open': 99.3,\n",
       "      'normal_glass_eye_close': 0.013,\n",
       "      'dark_glasses': 0.001},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.431,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.568,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 0.006},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.688,\n",
       "      'vector_z_component': 0.976,\n",
       "      'vector_x_component': 0.201,\n",
       "      'vector_y_component': 0.08,\n",
       "      'position_y_coordinate': 0.404},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.213,\n",
       "      'vector_z_component': 0.976,\n",
       "      'vector_x_component': 0.054,\n",
       "      'vector_y_component': 0.211,\n",
       "      'position_y_coordinate': 0.407}}},\n",
       "   'face_rectangle': {'width': 35, 'top': 257, 'left': 421, 'height': 35},\n",
       "   'face_token': 'f5ee1d3f20943c8951c27844819e625b'}]}"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "analyze_face(API_key,API_secret,face_token_02)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1bca744d",
   "metadata": {},
   "source": [
    "# python函数的方法，封装 face++ 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "8ac61942",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-24T14:05:29.482897Z",
     "start_time": "2022-03-24T14:05:29.478896Z"
    }
   },
   "outputs": [],
   "source": [
    "API_key = 'wH5jJXa327R2iOJ9TtgHkMXS4WHnfIzj'\n",
    "API_secret = 'APfd1pOaneXua_QcYfmcZEZrXfPfm1BM'\n",
    "face_token_01 = '297b39bb3dbd2224ea0d2dc6dc659bc7'\n",
    "face_token_02 = 'f5ee1d3f20943c8951c27844819e625b'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "60fc57b3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-24T14:06:05.085337Z",
     "start_time": "2022-03-24T14:06:05.073347Z"
    }
   },
   "outputs": [],
   "source": [
    "def compare_face(API_Key,API_Secret,face_token01,face_token02):\n",
    "    '''face++ API人脸对比函数'''\n",
    "    compare_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "    payload = {\n",
    "    \"api_key\":API_key,\n",
    "    \"api_secret\":API_secret,\n",
    "    \"face_token1\":face_token_01,\n",
    "    \"face_token2\":face_token_02\n",
    "\n",
    "    }\n",
    "    r_compare = requests.post(url = compare_url , params = payload)\n",
    "    return r_compare.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "27182b82",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-24T14:06:25.708778Z",
     "start_time": "2022-03-24T14:06:25.163780Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648130785,84970769-2838-4ab1-924d-c37f667fceb2',\n",
       " 'time_used': 325,\n",
       " 'confidence': 35.004,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compare_face(API_key,API_secret,face_token_01,face_token_02)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e9a920e",
   "metadata": {},
   "source": [
    "# 人脸库API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "d7f7347c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-24T14:38:52.681108Z",
     "start_time": "2022-03-24T14:38:51.890927Z"
    }
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "API_key = 'wH5jJXa327R2iOJ9TtgHkMXS4WHnfIzj'\n",
    "API_secret = 'APfd1pOaneXua_QcYfmcZEZrXfPfm1BM'\n",
    "\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'face_tokens':'f5ee1d3f20943c8951c27844819e625b'\n",
    "}\n",
    "\n",
    "\n",
    "r = requests.post(base_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "b54529a4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-24T14:38:52.728697Z",
     "start_time": "2022-03-24T14:38:52.715696Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "244bb066",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-24T14:38:53.602900Z",
     "start_time": "2022-03-24T14:38:53.595901Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '10d47cc40e583334e05b0ab49dca9fe3',\n",
       " 'time_used': 553,\n",
       " 'face_count': 1,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1648132732,62049ac2-895a-4260-9849-05497b49d968',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce8fd215",
   "metadata": {},
   "source": [
    "# face++ 人脸搜索API接口"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "8b8d40ae",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-24T14:40:01.096543Z",
     "start_time": "2022-03-24T14:40:00.499247Z"
    }
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "API_key = 'wH5jJXa327R2iOJ9TtgHkMXS4WHnfIzj'\n",
    "API_secret = 'APfd1pOaneXua_QcYfmcZEZrXfPfm1BM'\n",
    "\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/search'\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'image_url':'https://tse3-mm.cn.bing.net/th/id/OIP-C._MVPR376nHT7sR7DbefPcAHaHa?pid=ImgDet&rs=1',\n",
    "    'face_token':'f5ee1d3f20943c8951c27844819e625b',\n",
    "    'faceset_token':'10d47cc40e583334e05b0ab49dca9fe3'\n",
    "\n",
    "}\n",
    "\n",
    "\n",
    "r = requests.post(base_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "0fedf37a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-24T14:40:01.222133Z",
     "start_time": "2022-03-24T14:40:01.209226Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "b281c217",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-24T14:40:01.919895Z",
     "start_time": "2022-03-24T14:40:01.904888Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648132800,48123c35-934c-4d69-9177-d760425a5759',\n",
       " 'time_used': 380,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'results': [{'confidence': 97.389,\n",
       "   'user_id': '',\n",
       "   'face_token': 'f5ee1d3f20943c8951c27844819e625b'}]}"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d46761b2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
